#!/bin/bash
### The script  prepares the final yml files for all the services in the namespace
### The respective kubectl configs are in k8s/**/* location
### It is finally called in bin/k8s-review-deploy

cd "$(dirname "$0")"/..

## Function for replacing variables inside k8s yml files
function sed_files() {
    file_type=${1:-compose}
    file_name=$2
    echo "File Type is $file_type"

    if [[ "$file_type" == "deployment" ]]
    then
      echo "Applying SED on Deployment Yamls"
      echo "CI_REGISTRY_IMAGE : ${CI_REGISTRY_IMAGE}"
      sed -i "s~__CI_REGISTRY_IMAGE__~${CI_REGISTRY_IMAGE}~" $file_name
      echo "CI_ENVIRONMENT_SLUG: ${CI_ENVIRONMENT_SLUG}"
      sed -i "s/__CI_ENVIRONMENT_SLUG__/${CI_ENVIRONMENT_SLUG}/" $file_name
      echo "CI_COMMIT_REF_SLUG: ${CI_COMMIT_REF_SLUG}"
      sed -i "s~__VERSION__~${CI_COMMIT_REF_SLUG}~" $file_name
      echo "CI_PROJECT_NAME: ${CI_PROJECT_NAME}"
      sed -i "s~__CI_PROJECT_NAME__~${CI_PROJECT_NAME}~" $file_name
      echo "CI_PROJECT_PATH_SLUG : ${CI_PROJECT_PATH_SLUG}"
      sed -i "s~__CI_PROJECT_PATH_SLUG__~${CI_PROJECT_PATH_SLUG}~" $file_name
      echo "KUBE_INGRESS_BASE_DOMAIN : ${KUBE_INGRESS_BASE_DOMAIN}"
      sed -i "s/__KUBE_INGRESS_BASE_DOMAIN__/${KUBE_INGRESS_BASE_DOMAIN}/" $file_name
      sed -i "s/@GITLAB_GKE_DOMAIN@/$CI_ENVIRONMENT_SLUG.$KUBE_INGRESS_BASE_DOMAIN/" $file_name
      sed -i "s/@K8S_RUNNER_BEARER_TOKEN@/$K8S_RUNNER_BEARER_TOKEN/" $file_name
      #Using sed delimiter as = for K8S_RUNNER_HOST as the value contains / like https://"
      sed -i "s=@K8S_RUNNER_HOST@=$GITLAB_RUNNER_K8S_CLUSTER=" $file_name
    else
      echo "Applying SED on compose Yamls"
      sed -i "s~__CI_REGISTRY_IMAGE__~${CI_REGISTRY_IMAGE}~" docker-compose.yml
    fi
    ##Resource distribution separate for master and review branches
    if [[ "$CI_COMMIT_REF_SLUG" == "master" ]]
    then
      echo "Applying resources for $CI_COMMIT_REF_SLUG branch"
      #gitlab
      sed -i "s/@GITLAB_PV_CLAIM_CONFIG_SIZE@/20Gi/" $file_name
      sed -i "s/@GITLAB_PV_CLAIM_OPT_SIZE@/500Gi/" $file_name
      sed -i "s/@GITLAB_PV_CLAIM_LOG_SIZE@/50Gi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_LIMITS_MEMORY@/5000Mi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_LIMITS_CPU@/1500m/" $file_name
      sed -i "s/@GITLAB_RESOURCES_REQUESTS_MEMORY@/4500Mi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_REQUESTS_CPU@/1300m/" $file_name
      #redis
      sed -i "s/@REDIS_PV_CLAIM_SIZE@/5Gi/" $file_name
      sed -i "s/@REDIS_RESOURCES_LIMITS_MEMORY@/2000Mi/" $file_name
      sed -i "s/@REDIS_RESOURCES_LIMITS_CPU@/500m/" $file_name
      sed -i "s/@REDIS_RESOURCES_REQUESTS_MEMORY@/1800Mi/" $file_name
      sed -i "s/@REDIS_RESOURCES_REQUESTS_CPU@/400m/" $file_name
      #mlreefdb
      sed -i "s/@MLREEFDB_PV_CLAIM_SIZE@/50Gi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_LIMITS_MEMORY@/2000Mi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_LIMITS_CPU@/500m/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_REQUESTS_MEMORY@/1800Mi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_REQUESTS_CPU@/400m/" $file_name
      #gateway
      sed -i "s/@GATEWAY_RESOURCES_LIMITS_MEMORY@/600Mi/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_LIMITS_CPU@/200m/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_REQUESTS_MEMORY@/500Mi/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_REQUESTS_CPU@/100m/" $file_name
      #backend
      sed -i "s/@BACKEND_RESOURCES_LIMITS_MEMORY@/1000Mi/" $file_name
      sed -i "s/@BACKEND_RESOURCES_LIMITS_CPU@/500m/" $file_name
      sed -i "s/@BACKEND_RESOURCES_REQUESTS_MEMORY@/800Mi/" $file_name
      sed -i "s/@BACKEND_RESOURCES_REQUESTS_CPU@/300m/" $file_name
      sed -i "s/@GOOGLE_CLIENT_ID@/${GOOGLE_CLIENT_ID}/" $file_name
      sed -i "s/@GOOGLE_CLIENT_SECRET@/${GOOGLE_CLIENT_SECRET}/" $file_name
      sed -i "s/@GITHUB_CLIENT_ID@/${GITHUB_CLIENT_ID}/" $file_name
      sed -i "s/@GITHUB_CLIENT_SECRET@/${GITHUB_CLIENT_SECRET}/" $file_name
      sed -i "s/@FACEBOOK_CLIENT_ID@/${FACEBOOK_CLIENT_ID}/" $file_name
      sed -i "s/@FACEBOOK_CLIENT_SECRET@/${FACEBOOK_CLIENT_SECRET}/" $file_name
      sed -i "s/@LINKEDIN_CLIENT_ID@/${LINKEDIN_CLIENT_ID}/" $file_name
      sed -i "s/@LINKEDIN_CLIENT_SECRET@/${LINKEDIN_CLIENT_SECRET}/" $file_name
      sed -i "s/@GITLAB_CLIENT_ID@/${GITLAB_CLIENT_ID}/" $file_name
      sed -i "s/@GITLAB_CLIENT_SECRET@/${GITLAB_CLIENT_SECRET}/" $file_name
      sed -i "s/@FREELANCER_CLIENT_ID@/${FREELANCER_CLIENT_ID}/" $file_name
      sed -i "s/@FREELANCER_CLIENT_SECRET@/${FREELANCER_CLIENT_SECRET}/" $file_name
      sed -i "s/@LIVE_CLIENT_ID@/${LIVE_CLIENT_ID}/" $file_name
      sed -i "s/@LIVE_CLIENT_SECRET@/${LIVE_CLIENT_SECRET}/" $file_name
      sed -i "s/@AZURE_CLIENT_ID@/${AZURE_CLIENT_ID}/" $file_name
      sed -i "s/@AZURE_CLIENT_SECRET@/${AZURE_CLIENT_SECRET}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_ID@/${STACKEXCHANGE_CLIENT_ID}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_SECRET@/${STACKEXCHANGE_CLIENT_SECRET}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_KEY@/${STACKEXCHANGE_CLIENT_KEY}/" $file_name
      sed -i "s/@DEBUG_MODE@/${DEBUG_MODE}/" $file_name

      #gitlab-runner
      sed -i "s/@GITLAB_RUNNER_LIMITS_MEMORY@/52G/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_CPU@/1.9/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_SVC_MEMORY@/3G/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_SVC_CPU@/300m/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_HELPER_MEMORY@/700Mi/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_HELPER_CPU@/500m/" $file_name

    # Review branches
    else
      echo "Applying resources for $CI_COMMIT_REF_SLUG branch"
      #gitlab
      sed -i "s/@GITLAB_PV_CLAIM_CONFIG_SIZE@/10Gi/" $file_name
      sed -i "s/@GITLAB_PV_CLAIM_OPT_SIZE@/50Gi/" $file_name
      sed -i "s/@GITLAB_PV_CLAIM_LOG_SIZE@/30Gi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_LIMITS_MEMORY@/3500Mi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_LIMITS_CPU@/1200m/" $file_name
      sed -i "s/@GITLAB_RESOURCES_REQUESTS_MEMORY@/3000Mi/" $file_name
      sed -i "s/@GITLAB_RESOURCES_REQUESTS_CPU@/1000m/" $file_name
      #redis
      sed -i "s/@REDIS_PV_CLAIM_SIZE@/5Gi/" $file_name
      sed -i "s/@REDIS_RESOURCES_LIMITS_MEMORY@/1000Mi/" $file_name
      sed -i "s/@REDIS_RESOURCES_LIMITS_CPU@/100m/" $file_name
      sed -i "s/@REDIS_RESOURCES_REQUESTS_MEMORY@/500Mi/" $file_name
      sed -i "s/@REDIS_RESOURCES_REQUESTS_CPU@/80m/" $file_name
      #mlreefdb
      sed -i "s/@MLREEFDB_PV_CLAIM_SIZE@/10Gi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_LIMITS_MEMORY@/1000Mi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_LIMITS_CPU@/400m/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_REQUESTS_MEMORY@/800Mi/" $file_name
      sed -i "s/@MLREEFDB_RESOURCES_REQUESTS_CPU@/200m/" $file_name
      #gateway
      sed -i "s/@GATEWAY_RESOURCES_LIMITS_MEMORY@/500Mi/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_LIMITS_CPU@/100m/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_REQUESTS_MEMORY@/500Mi/" $file_name
      sed -i "s/@GATEWAY_RESOURCES_REQUESTS_CPU@/100m/" $file_name
      #backend
      sed -i "s/@BACKEND_RESOURCES_LIMITS_MEMORY@/900Mi/" $file_name
      sed -i "s/@BACKEND_RESOURCES_LIMITS_CPU@/400m/" $file_name
      sed -i "s/@BACKEND_RESOURCES_REQUESTS_MEMORY@/800Mi/" $file_name
      sed -i "s/@BACKEND_RESOURCES_REQUESTS_CPU@/200m/" $file_name
      sed -i "s/@GOOGLE_CLIENT_ID@/${GOOGLE_CLIENT_ID}/" $file_name
      sed -i "s/@GOOGLE_CLIENT_SECRET@/${GOOGLE_CLIENT_SECRET}/" $file_name
      sed -i "s/@GITHUB_CLIENT_ID@/${GITHUB_CLIENT_ID}/" $file_name
      sed -i "s/@GITHUB_CLIENT_SECRET@/${GITHUB_CLIENT_SECRET}/" $file_name
      sed -i "s/@FACEBOOK_CLIENT_ID@/${FACEBOOK_CLIENT_ID}/" $file_name
      sed -i "s/@FACEBOOK_CLIENT_SECRET@/${FACEBOOK_CLIENT_SECRET}/" $file_name
      sed -i "s/@LINKEDIN_CLIENT_ID@/${LINKEDIN_CLIENT_ID}/" $file_name
      sed -i "s/@LINKEDIN_CLIENT_SECRET@/${LINKEDIN_CLIENT_SECRET}/" $file_name
      sed -i "s/@GITLAB_CLIENT_ID@/${GITLAB_CLIENT_ID}/" $file_name
      sed -i "s/@GITLAB_CLIENT_SECRET@/${GITLAB_CLIENT_SECRET}/" $file_name
      sed -i "s/@FREELANCER_CLIENT_ID@/${FREELANCER_CLIENT_ID}/" $file_name
      sed -i "s/@FREELANCER_CLIENT_SECRET@/${FREELANCER_CLIENT_SECRET}/" $file_name
      sed -i "s/@LIVE_CLIENT_ID@/${LIVE_CLIENT_ID}/" $file_name
      sed -i "s/@LIVE_CLIENT_SECRET@/${LIVE_CLIENT_SECRET}/" $file_name
      sed -i "s/@AZURE_CLIENT_ID@/${AZURE_CLIENT_ID}/" $file_name
      sed -i "s/@AZURE_CLIENT_SECRET@/${AZURE_CLIENT_SECRET}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_ID@/${STACKEXCHANGE_CLIENT_ID}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_SECRET@/${STACKEXCHANGE_CLIENT_SECRET}/" $file_name
      sed -i "s/@STACKEXCHANGE_CLIENT_KEY@/${STACKEXCHANGE_CLIENT_KEY}/" $file_name
      sed -i "s/@DEBUG_MODE@/${DEBUG_MODE}/" $file_name
      #gitlab-runner
      sed -i "s/@GITLAB_RUNNER_LIMITS_MEMORY@/52G/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_CPU@/1.9/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_SVC_MEMORY@/3.5G/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_SVC_CPU@/300m/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_HELPER_MEMORY@/200Mi/" $file_name
      sed -i "s/@GITLAB_RUNNER_LIMITS_HELPER_CPU@/500m/" $file_name

    fi
  }

## Function to concanate multiple files in a file
scope_files() {
  for file in "$@"; do
    cat "$file"
    echo "---"
  done
}


## Check if the base domain variable is set
if [[ -z "$KUBE_INGRESS_BASE_DOMAIN" ]]; then
    echo "A needed variable is not exported. : GITLAB_GKE_DOMAIN"
    exit 1
fi

## Generate gitlab yml file
scope_files \
  k8s/gitlab/* \
  > gitlab-full.yml

echo "Using gitlab-${dashed_domain}.yml"
cp gitlab-full.yml gitlab-${dashed_domain}.yml
sed_files deployment gitlab-${dashed_domain}.yml

# Final gitlab yml file
cat gitlab-${dashed_domain}.yml


## Generate ingress yml file
scope_files \
  k8s/ingress/* \
  > ingress-full.yml
# Final ingress yml file
cp ingress-full.yml ingress-${dashed_domain}.yml
sed_files deployment ingress-${dashed_domain}.yml
cat ingress-${dashed_domain}.yml

## Generate app yml file
scope_files \
  k8s/apps/* \
  > apps-full.yml

echo "Using apps-${dashed_domain}.yml"
cp apps-full.yml apps-${dashed_domain}.yml
sed_files deployment apps-${dashed_domain}.yml

# Final app yml file
cat apps-${dashed_domain}.yml
